﻿<UserControl x:Class="Waf.MusicManager.Presentation.Views.PlaylistView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:p="clr-namespace:Waf.MusicManager.Presentation.Properties"
             xmlns:ctrl="clr-namespace:Waf.MusicManager.Presentation.Controls"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:dd="clr-namespace:Waf.MusicManager.Presentation.DesignData"
             mc:Ignorable="d" d:DataContext="{d:DesignInstance dd:SamplePlaylistViewModel, IsDesignTimeCreatable=True}"
             d:DesignHeight="500" d:DesignWidth="300" Background="{StaticResource DefaultBackground}">
    <UserControl.InputBindings>
        <KeyBinding Command="{Binding SearchNextCommand}" Key="F3" />
        <KeyBinding Command="{Binding SearchPreviousCommand}" Key="F3" Modifiers="Shift" />
    </UserControl.InputBindings>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Border BorderThickness="1,0,1,0" BorderBrush="{StaticResource DefaultBackground}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                
                <ctrl:SearchBox x:Name="searchBox" Grid.Column="0" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" HintText="{x:Static p:Resources.Search}"
                    Height="23" Padding="1,4,1,3" VerticalContentAlignment="Center" BorderThickness="0"/>
                <Button Grid.Column="1" Command="{Binding SearchPreviousCommand}" Content="&#57618;"
                        Foreground="{StaticResource LightForeground}" Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}" Style="{StaticResource InplaceButtonStyle}"/>
                <Button Grid.Column="2" Command="{Binding SearchNextCommand}" Content="&#57617;"
                        Foreground="{StaticResource LightForeground}" Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}" Style="{StaticResource InplaceButtonStyle}"/>
                <Button Grid.Column="3" Command="{Binding ClearSearchCommand}" 
                        Foreground="{StaticResource LightForeground}" Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}" Style="{StaticResource CloseButtonStyle}"/>
            </Grid>
        </Border>

        <ListBox x:Name="playlistListBox" Grid.Row="1" ItemsSource="{Binding PlaylistManager.Items}" SelectedItem="{Binding SelectedPlaylistItem}" SelectionMode="Extended" HorizontalContentAlignment="Stretch"
                 BorderThickness="0" Background="{StaticResource DefaultBackground}" AllowDrop="True"
                 ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 ctrl:SelectionBehavior.SyncSelectedItems="{Binding SelectedPlaylistItems}">
            <ListBox.InputBindings>
                <KeyBinding Command="{Binding PlaySelectedCommand}" Key="Return"/>
                <KeyBinding Command="{Binding RemoveSelectedCommand}" Key="Delete"/>
            </ListBox.InputBindings>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <Setter Property="Padding" Value="0,3,4,2"/>
                    <Setter Property="AllowDrop" Value="True"/>
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Command="{Binding PlaySelectedCommand}" Header="{x:Static p:Resources.Play}" InputGestureText="{x:Static p:Resources.Return}"/>
                                <MenuItem Command="{Binding RemoveSelectedCommand}" Header="{x:Static p:Resources.RemoveSelected}" InputGestureText="{x:Static p:Resources.Delete}"/>
                                <Separator/>
                                <MenuItem Command="{Binding ShowMusicPropertiesCommand}" Header="{x:Static p:Resources.ShowDetails}"/>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                    <EventSetter Event="ContextMenuOpening" Handler="ListBoxItemContextMenuOpening"/>
                    <EventSetter Event="MouseDoubleClick" Handler="ListBoxItemMouseDoubleClick"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="6"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="65"/>
                            <ColumnDefinition Width="3"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1"/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition Height="2"/>
                        </Grid.RowDefinitions>

                        <Border Background="{StaticResource ActiveForeground}" Width="3" HorizontalAlignment="Left" Grid.RowSpan="4">
                            <Border.Style>
                                <Style TargetType="Border">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Value="True">
                                            <DataTrigger.Binding>
                                                <MultiBinding Converter="{StaticResource IsPlaylistItemPlayingMultiConverter}">
                                                    <Binding Path="DataContext.PlaylistManager.CurrentItem" ElementName="playlistListBox"/>
                                                    <Binding Path="."/>
                                                </MultiBinding>
                                            </DataTrigger.Binding>
                                            <DataTrigger.Setters>
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger.Setters>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Border.Style>
                        </Border>

                        <ctrl:SearchableTextBlock Text="{Binding MusicFile.Metadata.Artists, Converter={StaticResource StringListToStringConverter}, ConverterParameter=ListSeparator}" 
                                SearchText="{Binding Text, ElementName=searchBox}"
                                FontWeight="SemiBold" TextTrimming="CharacterEllipsis" Grid.Row="1" Grid.Column="1" ctrl:ToolTipBehavior.AutoToolTip="True"
                                Visibility="{Binding MusicFile.LoadError, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=invert}">
                            <ctrl:SearchableTextBlock.Style>
                                <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                    <Style.Triggers>
                                        <DataTrigger Value="True">
                                            <DataTrigger.Binding>
                                                <MultiBinding Converter="{StaticResource IsPlaylistItemPlayingMultiConverter}">
                                                    <Binding Path="DataContext.PlaylistManager.CurrentItem" ElementName="playlistListBox"/>
                                                    <Binding Path="."/>
                                                </MultiBinding>
                                            </DataTrigger.Binding>
                                            <DataTrigger.Setters>
                                                <Setter Property="Foreground" Value="{StaticResource ActiveForeground}"/>
                                            </DataTrigger.Setters>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>   
                            </ctrl:SearchableTextBlock.Style>
                        </ctrl:SearchableTextBlock>
                        <TextBlock Text="{x:Static p:Resources.Error}" Grid.Row="1" Grid.Column="1" FontWeight="SemiBold" Foreground="{StaticResource ErrorForeground}"
                                   Visibility="{Binding MusicFile.LoadError, Converter={StaticResource NullToVisibilityConverter}}">
                            <TextBlock.ToolTip>
                                <ctrl:SuperToolTip Title="{x:Static p:Resources.InternalErrorDescription}" Description="{Binding MusicFile.LoadError.Message}"/>
                            </TextBlock.ToolTip>
                        </TextBlock>

                        <ctrl:SearchableTextBlock SearchText="{Binding Text, ElementName=searchBox}"
                            TextTrimming="CharacterEllipsis" Grid.Row="2" Grid.Column="1" ctrl:ToolTipBehavior.AutoToolTip="True">
                            <ctrl:SearchableTextBlock.Text>
                                <MultiBinding Converter="{StaticResource MusicTitleConverter}">
                                    <Binding Path="MusicFile.FileName"/>
                                    <Binding Path="MusicFile.Metadata.Artists"/>
                                    <Binding Path="MusicFile.Metadata.Title"/>
                                </MultiBinding>
                            </ctrl:SearchableTextBlock.Text>
                        </ctrl:SearchableTextBlock>

                        <ctrl:Rating Value="{Binding MusicFile.Metadata.Rating, Converter={StaticResource RatingToStarsConverter}}" Height="11"
                                   HorizontalAlignment="Right" Grid.Row="1" Grid.Column="2"/>
                        <TextBlock Text="{Binding MusicFile.Metadata.Duration, Converter={StaticResource DurationConverter}}" 
                                   HorizontalAlignment="Right" Grid.Row="2" Grid.Column="2"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Button x:Name="statusBarButton" Grid.Row="2" Click="StatusBarButtonClick" Height="21" Background="{StaticResource DarkBackground}" BorderThickness="0" HorizontalContentAlignment="Stretch">
            <Grid Margin="7,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding PlaylistManager.Items.Count, Converter={StaticResource ItemsCountConverter}}" Grid.Column="0"/>

                <TextBlock Text="&#xE10C;" FontFamily="Segoe UI Symbol" FontSize="10" VerticalAlignment="Center" Grid.Column="1"/>
                
                <TextBlock Grid.Column="2" HorizontalAlignment="Right">
                    <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource TotalDurationConverter}">
                            <Binding Path="PlaylistManager.IsTotalDurationEstimated"/>
                            <Binding Path="PlaylistManager.TotalDuration"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </Grid>
        </Button>
        <ctrl:Flyout x:Name="menuPopup" PlacementTarget="{Binding ElementName=statusBarButton}" Placement="Top">
            <Border BorderBrush="White" BorderThickness="0,1" Background="{StaticResource DarkBackground}">
                <Grid Margin="5,10,5,5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="80"/>
                        <ColumnDefinition Width="80"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Button Command="{Binding OpenListCommand}" Content="&#xE1A5;" Style="{StaticResource IconButtonStyle}" Grid.Row="0" Grid.Column="0"/>
                    <TextBlock Text="{x:Static p:Resources.OpenList}" Style="{StaticResource AppBarTextBlockStyle}" Grid.Row="1" Grid.Column="0"/>

                    <Button Command="{Binding SaveListCommand}" Content="&#xE105;" Style="{StaticResource IconButtonStyle}" Grid.Row="0" Grid.Column="1"/>
                    <TextBlock Text="{x:Static p:Resources.SaveList}" Style="{StaticResource AppBarTextBlockStyle}" Grid.Row="1" Grid.Column="1"/>

                    <Button Command="{Binding ClearListCommand}" Content="&#xE106;" Style="{StaticResource IconButtonStyle}" Grid.Row="0" Grid.Column="2"/>
                    <TextBlock Text="{x:Static p:Resources.ClearList}" Style="{StaticResource AppBarTextBlockStyle}" Grid.Row="1" Grid.Column="2"/>
                </Grid>
            </Border>
        </ctrl:Flyout>
    </Grid>
</UserControl>
